package priv.pront.code.national.acwing.dp.tree;

import java.util.*;
import java.io.*;
/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:49
 */
public class Acwing1074_二叉苹果树 {

    static int N, Q, m;
    static int[] h;
    static int[] e;
    static int[] ne;
    static int[] w;
    static int ans = 0;
    static int[][] f;
    static int[] v;
    static int idx = 0;
    static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
    public static void main(String[] args) throws IOException{
        String[] s = read.readLine().split("\\s+");
        N = Integer.parseInt(s[0]);
        Q = Integer.parseInt(s[1]);
        m = 2*N;
        h = new int[N+1];
        w = new int[m+1];
        e = new int[m+1];
        ne = new int[m+1];
        f = new int[N+1][N+1];
        v = new int[m+1];
        Arrays.fill(h, -1);
        for(int i = 1;i <= N - 1;i ++)
        {
            s = read.readLine().split("\\s+");
            int a = Integer.parseInt(s[0]);
            int b = Integer.parseInt(s[1]);
            int c = Integer.parseInt(s[2]);
            add(a,b,c);
            add(b,a,c);
        }
        dfs(1,-1);
        System.out.println(f[1][Q]);
    }

    private static void dfs(int u, int father){
        for(int i = h[u];i != -1;i = ne[i]) // 循环组
        {
            int child = e[i];
            if(child == father) continue;
            dfs(child, u);
            for(int j = Q;j >= 0;j --)  // 循环体积
            {
                for(int k = 0;k + 1<= j;k ++)  //循环决策
                    f[u][j] = Math.max(f[u][j], f[u][j - 1 - k] + f[child][k]+ w[i]);
            }
        }
    }

    private static void add(int a,int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

}
